package com.dingmouren.example.layoutmanagergroup.draggable;

import android.animation.ValueAnimator;
import android.view.View;

/**
 * this is animation class for rotate
 *
 * Created by sasakicks on 2015/09/09.
 */
public class FlipListener implements ValueAnimator.AnimatorUpdateListener {

    private View mParentView;
    private View mFrontView;
    private View mBackView;
    private boolean mFlipped;
    private int mDirection;

    public FlipListener(final View front, final View back, final View parent) {
        this.mParentView = parent;
        this.mFrontView = front;
        this.mBackView = back;
        this.mBackView.setVisibility(View.GONE);
    }

    @Override
    public void onAnimationUpdate(final ValueAnimator animation) {
        final float value = animation.getAnimatedFraction();
        final float scaleValue = 0.625f + (1.5f * (value - 0.5f) * (value - 0.5f));

        if (value <= 0.5f) {
            this.mParentView.setRotationX(180 * value * mDirection);   //设置View在X轴上的旋转角度
            if (mFlipped) setStateFlipped(false);
        } else {
            this.mParentView.setRotationX(-180 * (1 - value) * mDirection);
            if (!mFlipped) setStateFlipped(true);
        }
        //设置View在水平方向的缩放比例
        //scaleX=1，表示是原来的大小
        //scaleX<1，表示缩小，如scale=0.5f，表示宽度是原来的0.5倍
        //scaleX>1，表示放大，如scale=2.0f，表示宽度是原来的2.0倍
//        this.mParentView.setScaleX(scaleValue);
//        this.mParentView.setScaleY(scaleValue);
        this.mParentView.setScaleX(1);
        this.mParentView.setScaleY(1);

        this.mParentView.setPivotX(this.mParentView.getWidth()/2); //View旋转中心点的X坐标
        this.mParentView.setPivotY(this.mParentView.getHeight()/2);
    }

    public void reverse() {
        View temp = mBackView;
        mBackView = mFrontView;
        mFrontView = temp;
    }

    public void setRotateDirection(int direction) {
        mDirection = direction;
    }

    private void setStateFlipped(boolean flipped) {
        mFlipped = flipped;
        this.mFrontView.setVisibility(flipped ? View.GONE : View.VISIBLE);
        this.mBackView.setVisibility(flipped ? View.VISIBLE : View.GONE);
    }
}

